/*
 * RadioButtonMenuItem.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.properties.RadioButtonMenuItemProperties;

/**
 * RadioButton Menu Item.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class RadioButtonMenuItem extends MenuItem
                                 implements RadioButtonMenuItemProperties,
                                            Selectable {

// Static Initialization. ------------------------------------------------------

  static {
    _initialize();
  }

// Package-Private Fields. -----------------------------------------------------

  private boolean selected;
  private SelectableGroup selectableGroup;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new RadioButtonMenuItem object.
   */
  public RadioButtonMenuItem() {
    this(null);
  }

  /**
   * Constructs new RadioButtonMenuItem object.
   *
   * @param text the text
   */
  public RadioButtonMenuItem(String text) {
    super(text);
  }

// Selectable Interface Implementation. ----------------------------------------

  /**
   * {@inheritDoc}
   */
  public boolean isSelected() {
    return selected;
  }

  /**
   * {@inheritDoc}
   */
  public void setSelected(boolean selected) {
    if (this.selected != selected) {
      disposedCheck();
      _setSelected(_delegate, selected);
    }
  }

  /**
   * {@inheritDoc}
   */
  public SelectableGroup getSelectableGroup() {
    return selectableGroup;
  }

  /**
   * {@inheritDoc}
   */
  public void setSelectableGroup(SelectableGroup selectableGroup) {
    this.selectableGroup = selectableGroup;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected String paramString() {
    StringBuffer sb = new StringBuffer(super.paramString());
    if (selected) {
      sb.append(",selected");
    }
    return sb.toString();
  }

// Native Callbacks. -----------------------------------------------------------

  void _selectedChanged(int modifiers, boolean selected) {
    final boolean oldSelected = this.selected;
    if (oldSelected != selected) {
      this.selected = selected;
      firePropertyChange(PROPERTY_SELECTED, oldSelected, selected);
      fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
                                          getActionCommand(),
                                          System.currentTimeMillis(),
                                          modifiers));
      if ((selectableGroup != null) && selected) {
        selectableGroup.setSelected(this);
      }
    }
  }

// Package-Private Native Methods. ---------------------------------------------

  native long _create();

  native void _setText(long delegate, String text);

  native void _setShortcut(long delegate, String shortcut);

  native void _setIcon(long delegate, SwingIconSet iconSet);

  native void _setSelected(long delegate, boolean selected);

// Private Static Native Method. -----------------------------------------------

  private static native void _initialize();

}
